$(function () {
  $(".button >div:nth-child(1)").on('click',function () {
    login();
  }),
  $(".button >div:nth-child(2)").on('click',function () {
    window.open("register.html","_self")
  }),
    $("#username").on('keyup',function () {
        checkUsername();
    }),
    $("#password").on('keyup',function () {
       checkPassword();
    }),
      $("#refresh").on("click",function () {
        change()
    })
    $("#refresh").on("mouseover",function () {
        $("#refresh").css("text-decoration","underline")
    })
    $("#refresh").on("mouseleave",function () {
        $("#refresh").css("text-decoration","none")
    })


})
//切换验证码
function change() {
    console.log("awde")
    $("#pic > img").attr("src","http://localhost:8080/code?a="+Math.random())

}
function checkUsername() {
    let obj=$("#username");
    let username= obj.val();

    if(username==""){
        obj.attr({"placeholder":"请输入用户名"})
        obj.css("border-bottom","red 1px solid")
        return false;
    }
    obj.css("border","")
    return  true;
}

function checkPassword() {
    let obj=$("#password");
    let username= obj.val();

    if(username==""){
        obj.attr({"placeholder":"请输入密码"})
       obj.css("border-bottom","red 1px solid")
        return false;
    }
    obj.css("border","")
    return  true;
}


function  login() {

if(checkUsername() && checkPassword()) {

    let username= $("#username").val();
    let password=$("#password").val();
    let code=$("#code").val();
    let user = {username: username, password: password,code:code};
    $.ajax({
        url:"/login",
        data:user,
        type:"post",
        dataType:"json",
        success:function (data) {
            if(data.result=='success'){
                window.open("/html/manage/manage.html")
            }
            if(data.result=='code_error'){
                alert("验证码错误")
            }
            if(data.result=="failed"){
                alert("用户名或密码错误")
            }
        },
        error:function () {
            alert("服务器忙，请稍后再试")
        }

    })

}


}